home *** CD-ROM | disk | FTP | other *** search
/ The World of Computer Software / The World of Computer Software.iso / opstak.zip / OPSTACK.DOC < prev    next >
Text File  |  1990-10-30  |  3KB  |  88 lines

  1. OPSTACK - Unit for Monitoring Heap and Stack Usage
  2. -------------------------------------------------------
  3. Brian Foley
  4. TurboPower Software
  5. 11/90
  6. Version 1.1
  7. Released to the public domain
  8.  
  9. Overview
  10. ------------------------------------------------------------------------------
  11. OPSTACK allows you to accurately monitor a program's stack and heap usage
  12. simply by adding OPSTACK to the beginning of the program's USES list. The
  13. following program demonstrates how to use it:
  14.  
  15.   program OpStackTest;
  16.     {-Simple program to test OpStack}
  17.   uses
  18.     OpStack, Crt;
  19.   var
  20.     P : Pointer;
  21.  
  22.     procedure StackEater;
  23.       {-Use some stack space}
  24.     var
  25.       BigArray : array[1..12331] of Byte; {this is on the stack}
  26.     begin
  27.       {delay a moment to insure that OpStack sees what we've used}
  28.       Delay(10);
  29.     end;
  30.  
  31.   begin
  32.     {use some stack and heap space}
  33.     GetMem(P, 56789);
  34.     StackEater;
  35.   end.
  36.  
  37. If you run this little demo program, you'll see that OpStack automatically
  38. displays the amount of heap and stack space used by the program when it ends.
  39. In addition, it also shows the greatest amount of space used on the free list
  40. at any one time (under Turbo Pascal 4.0-5.5), and an estimate of the total
  41. amount of memory actually used by the program (for code, data, stack, and
  42. heap). This last value includes any unused memory set aside for the stack
  43. segment. It does not include the amount of space used for the free list (if
  44. any), nor does it include any heap space allocated for an overlay buffer.
  45.  
  46. Normally it is just this easy. In some cases, however, you may want or need to
  47. handle the reporting of results yourself. If so, you would set
  48. ReportStackUsage to False and call CalcStackUsage to obtain the necessary
  49. information.
  50.  
  51. If you want to disable stack monitoring temporarily--when executing other
  52. programs, for example--you can call RestoreInt8 to disable it and InstallInt8
  53. to reenable it.
  54.  
  55. OpStack can monitor only a single stack segment, normally the one in use when
  56. the program began. If you need to use it in a program that uses an alternate
  57. stack (a memory resident program, perhaps), you'll have to initialize three
  58. global variables before the first time that you switch to that stack. Here's
  59. an example:
  60.  
  61.    {disable OpStack momentarily}
  62.    RestoreInt8;
  63.  
  64.    {OurSS has the stack segment to watch}
  65.    OurSS := AlternateStackSegment;
  66.  
  67.    {InitialSP has the value of "SP" when the program began}
  68.    InitialSP := TopOfAlternateStack;
  69.  
  70.    {LowestSP has the lowest value of SP so far, same as InitialSP at first}
  71.    LowestSP := InitialSP;
  72.  
  73.    {reactivate OpStack}
  74.    InstallInt8;
  75.  
  76. Finally, in order to improve the accuracy of its results, OpStack reprograms
  77. the timer chip so that it can get control of the machine over 1000 times a
  78. second. For this reason, OpStack should not be used in programs that
  79. themselves reprogram the chip, or in programs that are using the PEP unit in
  80. Turbo Analyst. If you wish to change the rate at which samples are taken, you
  81. can call the SetSampleRate routine:
  82.  
  83.    {select 100 samples per second}
  84.    SetSampleRate(100);
  85.  
  86. The minimum value allowed is 18 samples per second, which is what the rate
  87. would be OpStack didn't reprogram the timer.
  88.